import { ActionContext } from 'vuex'
import { IStore } from '@/store/types'
import { local_storage } from '@/utils/storage'

export interface IAppState {
  showHelpFile: boolean
  subPackagesRoot: string
}
const state = {
  showHelpFile: false,
  subPackagesRoot: local_storage.get('LOCAL_ROOT_ROUTER_TYPE') || 'default'
}

const actions = {
  async handleHelpFileVisible({ commit }: ActionContext<IAppState, IStore>, params: boolean) {
    commit('SET_HELP_FILE_VISIBLE', params)
  },

  // 设置分包类型
  async setRootRouter({ commit }: ActionContext<IAppState, IStore>, params: string) {
    return new Promise((resolve, reject) => {
      local_storage.set('LOCAL_ROOT_ROUTER_TYPE', params)
      commit('SET_SUB_PACKAGES_ROOT', params)
      resolve(params)
    })
  }
}

const mutations = {
  // 设置全局存储的字典map
  SET_HELP_FILE_VISIBLE: (state: any, data: boolean) => {
    state.showHelpFile = data
  },

  SET_SUB_PACKAGES_ROOT: (state: IAppState, token: string) => {
    state.subPackagesRoot = token
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
